﻿namespace System.Xml.Serialization
{
    public abstract class XmlSerializer<T> : XmlAbstractSerializer, IXmlSerializer<T>
    {
        protected XmlSerializer()
        {
            Type type = typeof(T);
            XmlSerializers.Serializer[type] = this;
            InitializeRuntimeType(type);
        }

        public override object Deserialize(string value)
        {
            return (object)Read(value);
        }
        public override string Serialize(object value)
        {
            return Write((T)value);
        }

        protected abstract T Read(string value);
        protected abstract string Write(T value);

        T IXmlSerializer<T>.Deserialize(IXmlPropertyReader reader, string propertyName)
        {
            return Read(reader.Read(propertyName));
        }
        void IXmlSerializer<T>.Serialize(IXmlPropertyWriter writer, string propertyName, T value)
        {
            writer.Write(propertyName, Write(value));
        }
    }
}
